package com.hk.userservice.controller;

import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.Admin;
import com.hk.model.WxRole;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.userservice.service.WxRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/26 10:42
 * @Statement: 角色 前端控制器
 */
@RestController
@RequestMapping("/role")
public class WxRoleController {

    @Autowired
    private WxRoleService roleService;

    //    查询所有角色
    @GetMapping("/pageList")
    public ResultVO<Admin> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            WxRole role
    ) {
        List<WxRole> roleList = roleService.findAll(page, limit, role);
        PageInfo<WxRole> rolePageInfo = new PageInfo<>(roleList);
        int count = roleService.count(-1, -1, role);
        if (roleList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count, rolePageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count, rolePageInfo.getList());
        return resultVO;
//        return null;
    }

    // 添加角色
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result save(@RequestBody(required = false) WxRole role) {
        if (role == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        roleService.save(role);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @ApiOperation("通过Id查询角色详情")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        WxRole role = roleService.findById(id);
        if (role.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(role);
        return result;

    }

    //    todo 修改角色信息(已完毕)
    @ApiOperation("修改角色信息")
    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody WxRole role) throws IOException {
        roleService.update(role);
        Result result = CommonResult.successResult(null);
        return result;
    }


    //    todo 删除角色（已完毕）
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) throws IOException {
        int delete = roleService.delete(id);
        if (delete == 1) {
            Result result = CommonResult.successResult(null);
            return result;
        }
        Result result = CommonResult.nullResult(null);
        return result;
    }


}
